﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MS.InformationManagement.Core.Support;
using Microsoft.SharePoint;
using MS.InformationManagement.Support.Constants;

namespace MS.InformationManagement.BLL.DocumentPrinter
{
    public class UpdateReceivedDocumentWindow : IBaseCommand
    {

        #region fields

        SPFieldLookupValue _lookupValue;
        int _currentItemId;

        #endregion

        #region Constructors

        public UpdateReceivedDocumentWindow(SPFieldLookupValue lookupValue, int currentItemId)
        {
            _lookupValue = lookupValue;
            _currentItemId = currentItemId;
        }

        #endregion

        #region Implemented Command Methods
 
        public void Execute()
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite currentSite = new SPSite(SPContext.Current.Site.ID))
                {
                    using (SPWeb currentWeb = currentSite.OpenWeb())
                    {
                        currentWeb.AllowUnsafeUpdates = true;
                        currentSite.AllowUnsafeUpdates = true;
                        SPList currentList = currentWeb.Lists[GlobalConstants.LIST_DOCUMENTOS_VENTANILLA];
                        SPListItem currentItem = currentList.GetItemById(_currentItemId);
                        SPFieldLookupValueCollection lookUpMulti = currentItem[CustomSiteColumns.FIELD_WINDOW_RECEIVED_DELIVERY] as SPFieldLookupValueCollection;
                        if (lookUpMulti == null)
                            lookUpMulti = new SPFieldLookupValueCollection();
                        lookUpMulti.Add(_lookupValue);
                        currentItem[CustomSiteColumns.FIELD_WINDOW_RECEIVED_DELIVERY] = lookUpMulti;
                        currentItem.Update();
                        currentList.Update();
                        currentWeb.AllowUnsafeUpdates = false;
                        currentSite.AllowUnsafeUpdates = false;
                    }
                }
            });     
        }

        public void Undo()
        {
        }

        #endregion
    }
}
